﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Resources;
using Path = System.Windows.Shapes.Path;

namespace Sonce.Editor
{
	public partial class Editor
	{
		#region Public properties
		/// <summary>
		/// Gets a default cursor as visual element.
		/// </summary>
		public FrameworkElement DefaultCursor
		{
			get
			{
				return XamlReader.Load(
					@"<Path xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
						Width=""10"" Height=""10"" Stretch=""None"" StrokeLineJoin=""Round"" Stroke=""#FFF""
						Data=""M0,5 L10,5 M5,0 L5,10""/>") as FrameworkElement;
			}
		}
		#endregion

		#region Dependency properties
		/// <summary>
		/// Gets or sets a custom cursor as visual element.
		/// </summary>
		public FrameworkElement CustomCursor
		{
			get
			{
				FrameworkElement result = GetValue(CustomCursorProperty) as FrameworkElement;
				if (result == null)
				{
					result = this.DefaultCursor;
					SetValue(CustomCursorProperty, result);
				}
				return result;
			}
			set
			{
				SetValue(CustomCursorProperty, value);
			}
		}

		public static readonly DependencyProperty CustomCursorProperty = DependencyProperty.Register("CustomCursor", typeof(FrameworkElement), typeof(Editor), new PropertyMetadata(CustomCursorChanged));

		private static void CustomCursorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
		}
		#endregion

		#region Cursor cache
		/// <summary>
		/// Cached cursors as key and visual element.
		/// </summary>
		protected Dictionary<string, FrameworkElement> CursorCache = new Dictionary<string, FrameworkElement>();

		/// <summary>
		/// Clear cursor cache.
		/// </summary>
		protected void ClearCursorCache()
		{
			this.CursorCache.Clear();
		}
		#endregion

		#region Loading methods
		/// <summary>
		/// Loads a cursor from cache or from assembly resources.
		/// </summary>
		/// <param name="cursorName">Cursor name (e.g. "Cross").</param>
		/// <returns>Returns true on success.</returns>
		public bool LoadCursor(string cursorName)
		{
			//Check if cursor is cached
			if (this.CursorCache.ContainsKey(cursorName))
			{
				this.CustomCursor = this.CursorCache[cursorName];
				return (this.CursorCache[cursorName] != null);
			}
			else
			{
				string path = string.Format("Resources/Cursors/{0}.xaml", cursorName);
				Stream stream = Helper.GetResourceStream(path);
				return LoadCursor(cursorName, stream);
			}
		}

		public bool LoadCursor(string cursorName, Stream cursorStream)
		{
			FrameworkElement element = Helper.LoadXaml(cursorStream);
			bool completed = LoadCursor(cursorName, element);
			return completed;
		}

		public bool LoadCursor(string cursorName, FrameworkElement cursorElement)
		{
			this.CustomCursor = cursorElement;
			this.CursorCache[cursorName] = cursorElement; //Cache it under cursorName key
			return (cursorElement != null);
		}
		#endregion
	}
}
